<template>
  <nav class="navbar">
    <div class="container">
      <div class="logo">
        <router-link to="/">ShopVue</router-link>
      </div>
      <div class="nav-links">
        <router-link to="/" :class="{ active: $route.path === '/' }">首页</router-link>
        <router-link to="/products" :class="{ active: $route.path === '/products' }">商品</router-link>
        <router-link to="/about" :class="{ active: $route.path === '/about' }">关于我们</router-link>
        <router-link to="/contact" :class="{ active: $route.path === '/contact' }">联系我们</router-link>
      </div>

      <!-- 功能区域 -->
      <div class="actions">
        <!-- 搜索框 -->
        <div class="search">
          <input 
            type="text" 
            placeholder="搜索商品..." 
            v-model="searchQuery"
            @keyup.enter="handleSearch"
            @focus="isSearchFocused = true"
            @blur="isSearchFocused = false"
          >
          <button @click="handleSearch">
            <i class="fas fa-search"></i>
          </button>
        </div>

        <!-- 购物车 -->
        <router-link to="/cart" class="cart" v-if="userStore.isAuthenticated">
          <i class="fas fa-shopping-cart"></i>
          <span v-if="cartStore.itemCount > 0" class="cart-badge">{{ cartStore.itemCount }}</span>
        </router-link>

        <!-- 用户菜单 -->
        <div class="user-menu">
          <template v-if="userStore.isAuthenticated">
            <div class="user-info" @click="showUserMenu = !showUserMenu">
              <span>{{ userStore.userInfo.username }}</span>
              <i class="fas fa-caret-down"></i>
              
              <div class="dropdown" v-if="showUserMenu" :class="{ 'dropdown-enter': showUserMenu }">
                <router-link to="/profile">个人中心</router-link>
                <router-link to="/orders">我的订单</router-link>
                <button @click="handleLogout">退出登录</button>
              </div>
            </div>
          </template>
          
          <template v-else>
            <router-link to="/login" class="auth-link">登录</router-link>
            <router-link to="/register" class="auth-link">注册</router-link>
          </template>
        </div>

        <!-- 移动菜单 -->
        <button class="mobile-menu-btn" @click="showMobileMenu = !showMobileMenu" aria-label="菜单">
          <i class="fas" :class="showMobileMenu ? 'fa-times' : 'fa-bars'"></i>
        </button>
      </div>
    </div>
    <div class="mobile-menu" v-if="showMobileMenu" :class="{ 'mobile-menu-enter': showMobileMenu }">
      <router-link to="/" @click="showMobileMenu = false">首页</router-link>
      <router-link to="/products" @click="showMobileMenu = false">商品</router-link>
      <router-link to="/about" @click="showMobileMenu = false">关于我们</router-link>
      <router-link to="/contact" @click="showMobileMenu = false">联系我们</router-link>
      <router-link to="/cart" @click="showMobileMenu = false" v-if="userStore.isAuthenticated">购物车</router-link>
      <router-link to="/login" @click="showMobileMenu = false" v-if="!userStore.isAuthenticated">登录</router-link>
      <router-link to="/register" @click="showMobileMenu = false" v-if="!userStore.isAuthenticated">注册</router-link>
    </div>
  </nav>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { useCartStore } from '../stores/cart'

const userStore = useUserStore()
const cartStore = useCartStore()
const searchQuery = ref('')
const showUserMenu = ref(false)
const showMobileMenu = ref(false)
const isSearchFocused = ref(false) 
const router = useRouter()

// 搜索处理
const handleSearch = () => {
  if (searchQuery.value.trim()) {
    router.push({
      name: 'products',
      query: { search: searchQuery.value.trim() }
    })
  }
}

// 退出登录
const handleLogout = () => {
  userStore.logout()
  router.push('/login')
  showUserMenu.value = false
}
</script>

<style scoped>
//样式 
.navbar {
  background-color: #fff;
  border-bottom: 1px solid #eee;
  padding: 15px 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  position: sticky;
  top: 0;
  z-index: 1000;
  transition: box-shadow 0.3s ease;
}

.navbar:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Logo 样式 */
.logo a {
  font-size: 1.8rem;
  font-weight: 700;
  color: #42b983;
  text-decoration: none;
  letter-spacing: 0.5px;
  transition: color 0.3s ease, transform 0.3s ease;
}

.logo a:hover {
  color: #359e6d;
  transform: scale(1.02);
}

/* 导航链接样式 */
.nav-links {
  display: flex;
  gap: 25px;
  margin: 0 20px;
}

.nav-links a {
  color: #333;
  text-decoration: none;
  padding: 5px 0;
  position: relative;
  font-size: 1rem;
  font-weight: 500;
  transition: color 0.3s ease;
}

.nav-links a:hover:not(.active) {
  color: #42b983;
}

.nav-links a.active {
  color: #42b983;
}

.nav-links a.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #42b983;
  border-radius: 2px;
}

/* 功能区域样式 */
.actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* 搜索框样式 */
.search {
  display: flex;
  align-items: center;
  border: 1px solid #ddd;
  border-radius: 24px;
  padding: 6px 15px;
  transition: all 0.3s ease;
  background-color: #f9f9f9;
}

.search:focus-within,
.search:hover,
.search:has(input:focus) {
  border-color: #42b983;
  box-shadow: 0 0 0 2px rgba(66, 185, 131, 0.1);
  background-color: #fff;
}

.search input {
  border: none;
  outline: none;
  padding: 5px;
  min-width: 180px;
  background: transparent;
  font-size: 0.95rem;
}

.search button {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  padding: 3px;
  transition: color 0.3s ease;
}

.search button:hover {
  color: #42b983;
}

/* 购物车样式 */
.cart {
  color: #333;
  text-decoration: none;
  position: relative;
  font-size: 1.3rem;
  padding: 5px;
  border-radius: 50%;
  transition: color 0.3s ease, background-color 0.3s ease;
}

.cart:hover {
  color: #42b983;
  background-color: #f5f5f5;
}

.cart-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: #e74c3c;
  color: white;
  font-size: 0.7rem;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  box-shadow: 0 2px 4px rgba(231, 76, 60, 0.3);
}

/* 用户菜单样式 */
.user-menu {
  display: flex;
  align-items: center;
  gap: 15px;
}

.auth-link {
  color: #333;
  text-decoration: none;
  padding: 6px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
  font-size: 0.95rem;
}

.auth-link:hover {
  color: #42b983;
  background-color: #f5f5f5;
}

.user-info {
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.user-info span {
  font-size: 0.95rem;
  font-weight: 500;
}

.user-info i {
  transition: transform 0.3s ease;
}

.user-info:hover i {
  color: #42b983;
}

/* 下拉菜单样式 */
.dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  border: 1px solid #eee;
  border-radius: 8px;
  width: 160px;
  padding: 8px 0;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  opacity: 0;
  visibility: hidden;
  transform: translateY(5px);
  transition: all 0.3s ease;
}

.dropdown-enter {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.dropdown a, .dropdown button {
  display: block;
  width: 100%;
  padding: 10px 15px;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: none;
  color: #333;
  font-size: 0.95rem;
  transition: all 0.2s ease;
}

.dropdown a:hover, .dropdown button:hover {
  background-color: #f5f5f5;
  color: #42b983;
  padding-left: 18px;
}

/* 移动菜单按钮 */
.mobile-menu-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #333;
  display: none;
  cursor: pointer;
  padding: 5px;
  border-radius: 4px;
  transition: color 0.3s ease, background-color 0.3s ease;
}

.mobile-menu-btn:hover {
  color: #42b983;
  background-color: #f5f5f5;
}

/* 移动菜单样式 */
.mobile-menu {
  display: none;
  padding: 15px 20px;
  background-color: white;
  border-top: 1px solid #eee;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.3s ease, opacity 0.3s ease;
}

.mobile-menu-enter {
  max-height: 500px;
  opacity: 1;
}

.mobile-menu a {
  display: block;
  color: #333;
  text-decoration: none;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
  font-size: 1rem;
  transition: color 0.3s ease, padding-left 0.3s ease;
}

.mobile-menu a:last-child {
  border-bottom: none;
}

.mobile-menu a:hover {
  color: #42b983;
  padding-left: 5px;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .nav-links, .search {
    display: none;
  }
  
  .mobile-menu-btn, .mobile-menu {
    display: block;
  }
  
  .actions {
    gap: 10px;
  }
  
  .logo a {
    font-size: 1.5rem;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 10px;
  }
  
  .user-menu {
    gap: 10px;
  }
  
  .auth-link {
    padding: 5px 8px;
    font-size: 0.9rem;
  }
}
</style>